<?php
use yii\helpers\Html;
use yii\widgets\DetailView;
use yii\bootstrap\ActiveForm;

$models = $model->room; // Table OrderRoom
//$arr = \yii\helpers\ArrayHelper::map($models, 'id', 'id', 'room_id');
// 计算舱房数量
$arr = [];
$is_merge = false;
foreach($models as $v){
	if($model->product->stock == 1) {

        $name = $v->productRoom->room->name;
        $v->productRoom->room->code;

	}elseif($model->product->stock == 2){ // 海外航线
		$t = json_decode($model->product->rooms, true);	
		$name = isset($t[$v->room_id]['name']) ? $t[$v->room_id]['name'] : '异常'; 
	}
	$arr[$name][] = $v->id;

	//  是否有选择拼房
	if($model->is_merge == 2){
		if($v->merge_id){
			$is_merge = 1;
		}
	}
}
?>

<?=  yii\web\View::registerCssFile("/css/magic-check.css",['depends' => 'app\assets\AppAsset']) ?>

<div class="col-md-offset-1 col-md-10 panel panel-body" style="margin-top:30px;">
    <div class="page-header" style="margin-top:1px;padding-bottom:2px;">
		<p>
			<small>
			舱房共计 <?= count($models) ?> 间
			<?php if($arr){ ?>
			<?php foreach($arr as $key=>$val){ ?>
				 | <?= $key ?> <?= count($val)?> 间
			<?php }} ?>
			</small>
		</p>
	</div>
    <?php foreach($models as $mk=>$room){ ?>
    <div class="row" style="border-bottom: 1px solid #F1F1F1;">
        <div class="col-md-4" >
		<?php if($model->is_merge == 2){ ?>
			<?php if($is_merge){ ?>
				<?php if($room->merge_id){ ?> 
				<a class="fa fa-handshake-o" style="color:red;" href="/order/order/merge-list?id=<?= $model->id?>&room_id=<?= $room->id ?>" data-toggle='modal' data-target='.customer-modal' title='拼房标识' ></a>
				<?php } ?>
			<?php }else{  ?>
				<a class="fa fa-handshake-o"  href="/order/order/merge-list?id=<?= $model->id?>&room_id=<?= $room->id ?>" data-toggle='modal' data-target='.customer-modal' title='拼房标识' ></a>
		<?php } } ?>



		<a data-toggle="collapse" href="#collapseExample<?= $room->id?>" aria-expanded="false" aria-controls="collapseExample<?= $room->id?>" >
			<?php if($model->product->stock == 1) {?> 

        	    <?= $room->productRoom->room->name ?> 
        	    <?= $room->productRoom->room->code ?>
        	    <?= $room->productRoom->people ?>人间
        	    <!-- &nbsp;- </?= $room->num  ?>人 -->
			<?php }elseif($model->product->stock == 2){ // 海外航线
					$t = json_decode($model->product->rooms, true);	
					echo isset($t[$room->room_id]['name']) ? $t[$room->room_id]['name'] : '异常'; 
					echo "  - ".$room->num . "人";
				}
			?>
		</a>
        </div>
        <div class="col-md-6">
            <?php $exit = yii\helpers\ArrayHelper::map($room->orderRoomCustomer, 'id', 'customer_id');?>
            <?php foreach($customers as $customer){ ?>
            <div style="float:left;margin-right:15px;width:90px;">
                <input type="hidden" class="room-id" value="<?= $room->id?>">
                <?php if(in_array($customer->id,$exit)){ ?>
                <input class="magic-checkbox room-magic" <?= in_array($customer->id, $exit) ? 'checked' : '' ?> type="checkbox" id="room<?= $mk.$customer->id?>" name="<?= $customer->id?>">
                	<a data-toggle="modal" data-target=".customer-modal"  title="编辑该用户" href="/order/customer/update?id=<?= $customer->id?>" data="<?= $customer->id ?>" class="">
						<?php $name = $customer->first_name.$customer->last_name; ?>
						<?= empty($name) ? '--' : $name ?>
					</a>
					</label>
                <?php }?>
            </div>
            <?php } ?>
        </div>
		<div class="col-md-2">
		</div>
		<!-- 舱房的 一些辅助信息 -->
		<div class="col-md-12 collapse" id="collapseExample<?= $room->id?>">
		    <div class="alert row">
		  		<div class="col-md-2">金额 : <?= $room->price ?></div>
		  		<div class="col-md-2">入住人数 : <?= $room->num ?>人</div>
		  		<div class="col-md-2">房号: <?= empty($room->code) ? "未设置" : $room->code ?></div>
		    </div>
		</div>

    </div>
    </br>
    <?php } ?>
</div>

<!-- 取消的舱房 -->
<?php if($model->refund ){ ?>
<div class="col-md-offset-1 col-md-10 panel panel-body" style="margin-top:10px;">
	<h4 class="page-header" style="margin-top:5px;"><small class="text-warning">取消舱房列表</small></h4>
	<table class="table" style="color: #999;">  
		<tr>
			<td>房型</td>
			<td>舱房金额</td>
			<td>扣除金额</td>
			<td>退款金额</td>
			<td>取消原因</td>
			<td>审核</td>
		</tr>

		<?php foreach($model->refund as $refund) { ?>
		<tr>
			<td class="col-md-4">
				<?= $refund->room ?>
			</td>
			<td><?= $refund->room_price ?></td>
			<td><?= $refund->price ?></td>
			<td><?= $refund->room_price-$refund->price ?></td>
			<td><?= $refund->desc ?></td>
			<td><label class="label <?= ($refund->status==2) ? 'label-success' : 'label-danger' ?>"><?= \Yii::$app->params['refund'][$refund->status] ?></label></td>
		</tr>
		<?php } ?>
	</table>
</div>
<?php } ?>

<div class="modal fade customer-modal" role="dialog" aria-labelledby="myLargeModalLabel">
  <div class="modal-dialog " role="document" style="width:1060px;">
      <div class="modal-content">
	 		<!-- 嵌入内容 --> 
	  </div>
  </div>
</div>




<div class="modal fade refund-modal" role="dialog" aria-labelledby="myLargeModalLabel">
  <div class="modal-dialog " role="document" >
      <div class="modal-content">
  	  	  <div class="modal-header">
  	  	  	  <button type="button" class="close" data-dismiss="modal" aria-label="Close">
	  	      	<span aria-hidden="true">&times;</span>
	  	      </button>
  	  	  	  <h4 class="modal-title">订单舱房取消</h4>
  	  	  </div>
      	  <div class="modal-body">
		  	  <textarea id="order-refund" class="form-control" placeholder="取消原因" maxlength="255" rows="4"></textarea>
			  <input type="hidden" id="refund-room-id" value="">
		  </div>
		  <div class="modal-footer">
		  	  <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
		  	  <button type="button" class="btn btn-primary" id="remove-room" >确定</button>
		  </div>
	  </div>
  </div>
</div>



<?php app\components\JsBlock::begin() ?>
<script type="text/javascript">
    /**  add Loading  **/
    $(".room-magic").click(function(){
        var customer_id   = $(this).attr('name'); // 获取客户编号
        var order_room_id = $(this).siblings('.room-id').val(); // 获取客户编号
        var checked = $(this).is(':checked');   // 是否选中
        
        $.get("/order/room-customer/save", {customer_id:customer_id,order_room_id:order_room_id,checked:checked}, function(data){
			console.log(data);

			if(data == 'order-price-exit'){
				alert("该用户已分配附加费不能取消, 请先从附加费中移除该用户");	
				window.location.reload();
			}else if(data == 'order-assign-exit'){

				alert("该用户所在订单已经分团不能取消, 请先从分团中移除对应订单");	
				window.location.reload();
			}

        });
    });
	// 定位删除订单
    $(".refund").click(function(){
        var id = $(this).data('id'); // 房间 order_room 表 ID

		$("#refund-room-id").val(id);
		//$("#remove-room").text(id);
	});

    $("#remove-room").click(function(){
        // var id = $(this).data('id'); // 房间 order_room 表 ID
		var id = $("#refund-room-id").val();
		var desc = $("#order-refund").val();

        $.get("/order/room/remove", {id: id, desc: desc}, function(data){
			//console.log(data);
			//alert(data);
			if(data == 'true'){

				 window.location.reload();
			}else if(data == 'error1'){

				alert("请先取消舱房填写的入住人");
			}else{
			
			}
        });
    });

	// 清除模态框缓存
	$(".customer-modal").on("hidden.bs.modal", function() {
		$(this).removeData("bs.modal");
		//window.location.reload();
	});
</script>
<?php app\components\JsBlock::end() ?>
